package com.example.langchain4j.controller;

import com.example.langchain4j.common.Result;
import com.example.langchain4j.service.JiTiYiAnService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;

@RestController
@RequestMapping("/api/JiTi")
@Tag(name = "行政处罚决定书接口")
@Slf4j
public class JiTiYiAnController {

    @Resource
    private JiTiYiAnService jitiyiAnService;

    private static final String GENERATED_DIR = "src/main/resources/generated";

    /**
     * 生成集体议案
     */
    @Operation(summary = "生成集体议案")
    @PostMapping("/generate-集体议案")
    public Result<Map<String, Object>> generatePenaltyDecision(@RequestBody Map<String, String> request,
                                                               HttpServletResponse response) {

        try {
            String caseApprovalJson = request.get("caseApprovalJson");
            String interrogationJson = request.get("interrogationJson");

            if (caseApprovalJson == null || caseApprovalJson.isEmpty()) {
                return Result.error("缺少立案审批表JSON数据");
            }



//            return Result.success(result);
        } catch (Exception e) {
            log.error("生成行政处罚决定书失败", e);
            return Result.error("生成行政处罚决定书失败: " + e.getMessage());
        }


        return null;
    }



    /**
     * 下载生成的文件
     */
    @Operation(summary = "下载生成的处罚决定书文件")
    @GetMapping("/generated/{fileName:.+}")
    public void downloadGeneratedFile(
            @PathVariable String fileName,
            HttpServletResponse response) throws IOException {

        log.info("下载生成文件: {}", fileName);

        Path filePath = Paths.get(GENERATED_DIR, fileName);

        if (!Files.exists(filePath)) {
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
            return;
        }

        response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
        response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");

        try (FileInputStream fis = new FileInputStream(filePath.toFile())) {
            fis.transferTo(response.getOutputStream());
            response.getOutputStream().flush();
        }
    }
}
